package com.problem.leetcode;

import com.google.common.base.Stopwatch;

/**
 * @author didi
 * @DESCRIPTION
 * @create 2019-05-14 15:38
 **/

public class Problem832 {
    public int[][] flipAndInvertImage(int[][] A) {
        //先遍历行 再遍历列
        for (int rowIndex = 0; rowIndex < A.length; rowIndex++) {
            int begin = 0, end = A[0].length - 1;
            while (begin <= end) {

                if (begin == end) {
                    A[rowIndex][begin] = converse(A[rowIndex][begin]);
                    break;
                }

                A[rowIndex][begin] = converse(A[rowIndex][begin]);
                A[rowIndex][end] = converse(A[rowIndex][end]);
                int tmp = A[rowIndex][begin];
                A[rowIndex][begin] = A[rowIndex][end];
                A[rowIndex][end] = tmp;
                ++begin;
                --end;
            }
        }
        return A;
    }

    private int converse(int number) {
        return number == 0 ? 1 : 0;
    }

    public static void main(String[] args) {
        Problem832 problem832 = new Problem832();
        Stopwatch  stopwatch  = Stopwatch.createStarted();
        for (int index = 0; index < 5000000; index++) {
            problem832.flipAndInvertImage(new int[][]{{1, 1, 0, 0}, {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 1, 0}});
        }
        stopwatch.stop();
        System.out.println(stopwatch);
    }

}
